#!python3

from bs4 import BeautifulSoup
# import wget
import re
# import urllib.request
from util import execute
import os.path

# url = "https://www.nhk.or.jp/lesson/english/download/"

# source = urllib.request.urlopen(url).read()
# $ curl $url > nhk.txt

prefix = "https://www.nhk.or.jp/lesson/english/download"
host = "https://www.nhk.or.jp"
source = open("nhk.txt")

soup = BeautifulSoup(source, 'lxml')
href = soup.select("li.dl-mp3 a[href]")
downloadUrls = []
for anchor in href[0:2]:
    path = anchor.get('href')
    fileName = re.split(r"/", path)[-1]
    if os.path.exists(fileName):
        continue
    print(">>>%s" % fileName)
    downloadUrl = "%s%s" % (host, path)
    print(downloadUrl)
    # wget.download(downloadUrl, fileName)
    downloadUrls.append(downloadUrl)

cmd = "curl -O %s" % (" ".join(downloadUrls))
execute(cmd)
